<template>
	<van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate"
		@cancel="$emit('close')" @confirm="onConfirm" />
</template>

<script>
	import {
		updateUserProfile
	} from '@/api/user.js'
	import dayjs from 'dayjs'

	export default {
		props: {
			value: {
				type: String,
				required: true
			}
		},

		data() {
			return {
				minDate: new Date(1970, 0, 1),
				maxDate: new Date(),
				currentDate: new Date(this.value),
			};
		},
		methods: {
			async onConfirm(value, index) {
				try {
					const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')
					await updateUserProfile({
						birthday: currentDate
					})

					this.$emit('input', currentDate)
					this.$emit('close')
				} catch (e) {
					this.$toast('更新生日失败')
				}
			},
		},
	}
</script>

<style scoped>

</style>
